var app            = getApp()
var operate_count  = 6
var operate_expire = 2160000

function addOperateCount(name, type = '') {
  var op = wx.getStorageSync('$' + name)

  wx.setStorageSync('$' + name, {count:op.count + 1, time:op.time, type:type})
}

function checkOperateCount(name, type = '') {
  var op = getOperateInfo(name, type)

  if (op.time + operate_expire < (new Date()).getTime()) {
    op = initOperateInfo(name, type)
  }

  if (op.count < operate_count) {
    addOperateCount(name, type)
    return true
  }
  return false
}

function initOperateInfo(name, type = '') {
  var time = (new Date()).getTime()
  wx.setStorageSync('$' + name, {type:type, time:time, count:0})
  return {type:type, time:time, count:0}
}

function getOperateInfo(name, type) {
  var op = wx.getStorageSync('$' + name)

  if (op) {
    return op
  }
  return initOperateInfo(name, type)
}

function clearOperateInfo(name, type = '') {
  wx.removeStorageSync('$' + name)
}

// function initOperateInfo(name, type = '') {
//   // body...
// }

module.exports = {
  checkOperateCount: checkOperateCount,
  clearOperateInfo : clearOperateInfo
}